Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Text
Imports System.Reflection

<Designer(GetType(SmartTagDesigner)), _
System.ComponentModel.DefaultBindingProperty("SelectedPath"), _
DefaultProperty("SelectedPath")> _
Public Class FolderPicker

  Private _selectOnFocus As Boolean

#Region "Public Interface"

  <System.ComponentModel.Category("Behavior")> _
  Public Overrides Property ContextMenu() As System.Windows.Forms.ContextMenu
    Get
      Return Me.SelectedPathtextbox.ContextMenu
    End Get

    Set(ByVal value As System.Windows.Forms.ContextMenu)
      Me.SelectedPathtextbox.ContextMenu = value

    End Set
  End Property

  <System.ComponentModel.Category("Folder Browsing"), DefaultValue("")> _
   Public Property Description() As String
    Get
      Return Me.FolderBrowserDialog1.Description
    End Get

    Set(ByVal value As String)
      Me.FolderBrowserDialog1.Description = value

    End Set
  End Property

  <System.ComponentModel.Category("Folder Browsing"), DefaultValue(True)> _
  Public Property ShowNewFolderButton() As Boolean
    Get
      Return Me.FolderBrowserDialog1.ShowNewFolderButton
    End Get

    Set(ByVal value As Boolean)
      Me.FolderBrowserDialog1.ShowNewFolderButton = value

    End Set
  End Property

  <System.ComponentModel.Category("Data"), DefaultValue("")> _
  Public Property SelectedPath() As String
    Get
      Return SelectedPathtextbox.Text
    End Get
    Set(ByVal value As String)
      SelectedPathtextbox.Text = value
    End Set
  End Property


  <System.ComponentModel.Category("Behavior"), DefaultValue(250)> _
  Public Property MaxLength() As Integer
    Get
      Return Me.SelectedPathtextbox.MaxLength
    End Get

    Set(ByVal value As Integer)
      Me.SelectedPathtextbox.MaxLength = value

    End Set
  End Property

  <System.ComponentModel.Category("Behavior"), DefaultValue(False)> _
Public Property Multiline() As Boolean

    Get
      Return SelectedPathtextbox.Multiline
    End Get

    Set(ByVal value As Boolean)
      SelectedPathtextbox.Multiline = value

      If value = True Then
        SelectedPathtextbox.Height = Me.Height

      ElseIf value = False Then
        Me.Height = SelectedPathtextbox.Height

      End If

    End Set

  End Property

  <System.ComponentModel.Category("Appearance"), DefaultValue(GetType(System.Windows.Forms.ScrollBars), "None")> _
Public Property ScrollBars() As System.Windows.Forms.ScrollBars
    Get
      Return Me.SelectedPathtextbox.ScrollBars
    End Get

    Set(ByVal value As System.Windows.Forms.ScrollBars)
      Me.SelectedPathtextbox.ScrollBars = value

    End Set
  End Property

  <System.ComponentModel.Category("Behavior"), DefaultValue(False)> _
Public Property SelectOnFocus() As Boolean
    Get
      Return _selectOnFocus
    End Get

    Set(ByVal value As Boolean)
      _selectOnFocus = value

    End Set
  End Property

  <System.ComponentModel.Category("Appearance"), DefaultValue(GetType(System.Windows.Forms.HorizontalAlignment), "Left")> _
Public Property TextAlign() As System.Windows.Forms.HorizontalAlignment
    Get
      Return Me.SelectedPathtextbox.TextAlign
    End Get

    Set(ByVal value As System.Windows.Forms.HorizontalAlignment)
      Me.SelectedPathtextbox.TextAlign = value

    End Set
  End Property

  <System.ComponentModel.Category("Appearance"), DefaultValue(GetType(System.Environment.SpecialFolder), "Desktop")> _
Public Property RootFolder() As System.Environment.SpecialFolder
    Get
      Return Me.FolderBrowserDialog1.RootFolder
    End Get

    Set(ByVal value As System.Environment.SpecialFolder)
      Me.FolderBrowserDialog1.RootFolder = value

    End Set
  End Property


#End Region

#Region "Private Event Handlers"

  Private Sub SelectedPathtextbox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles SelectedPathtextbox.Enter

    If Me._selectOnFocus Then
      SelectedPathtextbox.SelectAll()
    End If

  End Sub

  Private Sub EllipsisButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EllipsisButton.Click

    If Me.FolderBrowserDialog1.ShowDialog(Me) = DialogResult.OK Then
      Me.SelectedPathtextbox.Text = FolderBrowserDialog1.SelectedPath
    End If

  End Sub

#End Region

End Class

#Region "Smart Tag Support"

'///////////////////////////////////////////////////////////////
'Designer for the ColorLabel control with support for a smart 
' tag panel.
'///////////////////////////////////////////////////////////////
'Must add reference to System.Design.dll
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class SmartTagDesigner
  Inherits System.Windows.Forms.Design.ControlDesigner

  Private lists As DesignerActionListCollection

  'Use pull model to populate smart tag menu.
  Public Overrides ReadOnly Property ActionLists() _
  As DesignerActionListCollection
    Get
      If lists Is Nothing Then
        lists = New DesignerActionListCollection()
        lists.Add( _
        New SmartTagActionList(Me.Component))
      End If
      Return lists
    End Get
  End Property
End Class

'///////////////////////////////////////////////////////////////
'DesignerActionList-derived class defines smart tag entries and
' resultant actions.
'///////////////////////////////////////////////////////////////
Public Class SmartTagActionList
  Inherits System.ComponentModel.Design.DesignerActionList

  Private _FolderPicker As FolderPicker

  <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")> _
  Private designerActionUISvc As DesignerActionUIService

  'The constructor associates the control 
  'with the smart tag list.
  Public Sub New(ByVal component As IComponent)

    MyBase.New(component)
    Me._FolderPicker = component

    ' Cache a reference to DesignerActionUIService, so the
    ' DesigneractionList can be refreshed.
    Me.designerActionUISvc = CType(GetService(GetType(DesignerActionUIService)), DesignerActionUIService)

  End Sub

  'Helper method to retrieve control properties. Use of 
  ' GetProperties enables undo and menu updates to work properly.
  Private Function GetPropertyByName(ByVal propName As String) _
  As PropertyDescriptor
    Dim prop As PropertyDescriptor
    prop = TypeDescriptor.GetProperties(_FolderPicker)(propName)
    If prop Is Nothing Then
      Throw New ArgumentException( _
      "Matching property not found!", propName)
    Else
      Return prop
    End If
  End Function


  Public Property SelectedPath() As String
    Get
      Return _FolderPicker.SelectedPath
    End Get
    Set(ByVal value As String)
      GetPropertyByName("SelectedPath").SetValue(_FolderPicker, value)
    End Set
  End Property

  Public Property Multiline() As Boolean
    Get
      Return _FolderPicker.Multiline
    End Get
    Set(ByVal value As Boolean)
      GetPropertyByName("Multiline").SetValue(_FolderPicker, value)
    End Set
  End Property

  'Implementation of this virtual method creates smart tag  
  ' items, associates their targets, and collects into list.
  Public Overrides Function GetSortedActionItems() _
  As DesignerActionItemCollection
    Dim items As New DesignerActionItemCollection()

    items.Add( _
    New DesignerActionPropertyItem( _
    "SelectedPath", _
    "Selected Path", _
    "", _
    "Sets the folder path"))

    items.Add( _
    New DesignerActionPropertyItem( _
    "Multiline", _
    "Multiline", _
    "", _
    "Sets Text to be Multiline."))

    Return items
  End Function

End Class

#End Region


